﻿using CgdataBase;
using System.Diagnostics;
using System.Windows;
using System.Windows.Media;

namespace FolderCompare.Views
{
    public partial class MessageBox : Window
    {
        public MessageBoxResult Result { get; set; }
        private AppSettings Settings => AppSettings.Instance;

        public MessageBox(string messageBoxText, string caption, MessageBoxButton button)
        {
            InitializeComponent();
            this.Title = caption;
            tbkContent.Text = messageBoxText;
            this.FontSize = Settings.FontSize;
            this.FontFamily = new FontFamily(Settings.FontName);
            WindowController.EscToExit(this);

            switch (button)
            {
                case MessageBoxButton.OK:
                    btnCancel.Visibility = Visibility.Collapsed;
                    btnYes.Visibility = Visibility.Collapsed;
                    btnNo.Visibility = Visibility.Collapsed;
                    break;
                case MessageBoxButton.OKCancel:
                    btnYes.Visibility = Visibility.Collapsed;
                    btnNo.Visibility = Visibility.Collapsed;
                    break;
                case MessageBoxButton.YesNoCancel:
                    btnOK.Visibility = Visibility.Collapsed;
                    break;
                case MessageBoxButton.YesNo:
                    btnOK.Visibility = Visibility.Collapsed;
                    btnCancel.Visibility = Visibility.Collapsed;
                    break;
                default:
                    Debug.Assert(false);
                    break;
            }
        }

        private void btnYes_Click(object sender, RoutedEventArgs e)
        {
            Result = MessageBoxResult.Yes;
            this.DialogResult = true;
        }

        private void btnNo_Click(object sender, RoutedEventArgs e)
        {
            Result = MessageBoxResult.No;
            this.DialogResult = true;
        }

        private void btnOK_Click(object sender, RoutedEventArgs e)
        {
            Result = MessageBoxResult.OK;
            this.DialogResult = true;
        }

        public static MessageBoxResult Show(string messageBoxText)
        {
            return Show(messageBoxText, "提示", MessageBoxButton.OK);
        }

        public static MessageBoxResult Show(string messageBoxText, string caption)
        {
            return Show(messageBoxText, caption, MessageBoxButton.OK);
        }

        public static MessageBoxResult Show(string messageBoxText, string caption, MessageBoxButton button)
        {
            return App.Current.Dispatcher.Invoke<MessageBoxResult>(() =>
            {
                var win = new MessageBox(messageBoxText, caption, button);
                var owner = App.GetActiveWindow();
                if (owner != null)
                {
                    win.Owner = owner;
                }
                if (win.ShowDialog() == true)
                    return win.Result;

                return MessageBoxResult.Cancel;
            });
        }
    }
}